<?php
if(isset($_SESSION['which']) and $_SESSION['which'] == 'student')
{
	$offset = 0;
	$num_of_rows = 5;

	/// pages operation
	$sql = "SELECT COUNT(*) FROM sinh_vien";
	$result = mysqli_query($link, $sql);
	if(!$result)
	{
		$error = 'Error fetching count of students';
		include '/includes/error.html.php';
		exit();
	}
	$row = mysqli_fetch_array($result);
	$count = $row[0];
	$pages = ceil($count / $num_of_rows);

	if(isset($_SESSION['index']))
	{
		// this if is for 'delete' case
		if($_SESSION['index'] >= $pages)
		$offset = max(0, $pages - 1);
		// this else is for 'change page' case
		else
		$offset = $_SESSION['index'] * $num_of_rows;
	}

	$_SESSION['pages'] = $pages;
}
else if(isset($_SESSION['which']) and $_SESSION['which'] == 'subject')
{
	// prepare pages
	$offset = 0;
	$num_of_rows = 5;

	/// pages operation
	$sql = "SELECT COUNT(*) FROM mon_hoc";
	$result = mysqli_query($link, $sql);
	if(!$result)
	{
		$error = 'Error fetching count of subjects';
		include './includes/error.html.php';
		exit();
	}
	$row = mysqli_fetch_array($result);
	$count = $row[0];
	$pages = ceil($count / $num_of_rows);

	if(isset($_SESSION['index']))
	{
		// this if is for 'delete' case

		if($_SESSION['index'] >= $pages)
		$offset = max(0, $pages - 1);
		// this else is for 'change page' case
		else
		$offset = $_SESSION['index'] * $num_of_rows;
	}

	$_SESSION['pages'] = $pages;
}
else if(isset($_SESSION['which']) and $_SESSION['which'] == 'score')
{
	// prepare pages
	$offset = 0;
	$num_of_rows = 5;

	/// pages operation
	$student_id = $_SESSION['student_id'];
	$sql = "SELECT COUNT(*) FROM diem WHERE mssv = '$student_id'";
	$result = mysqli_query($link, $sql);
	if(!$result)
	{
		$error = 'Error fetching count of scores';
		include './includes/error.html.php';
		exit();
	}
	$row = mysqli_fetch_array($result);
	$count = $row[0];
	$pages = ceil($count / $num_of_rows);

	if(isset($_SESSION['index']))
	{
		// this if is for 'delete' case
		if($_SESSION['index'] >= $pages)
		$offset = max(0, $pages - 1);
		// this else is for 'change page' case
		else
		$offset = $_SESSION['index'] * $num_of_rows;
	}

	$_SESSION['pages'] = $pages;
}
?>